package com.akk.action;

import com.akk.business.AvsHkPayBusiness;
import com.akk.module.avs.model.request.HkPaymentNotifyRequest;
import com.akk.module.avs.service.BankService;
import com.akk.module.avs.service.CityService;
import com.akk.module.hkpay.model.HkACreateOrderRequest;
import com.akk.module.hkpay.model.HkCreateWechatOrderRequest;
import com.akk.module.hkpay.model.HkSubmitMerPhotoRequest;
import com.akk.module.hkpay.model.HkSubmitRequest;
import com.akk.utils.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;


/**
 * Created by admin on 2017/11/11.
 * 汇卡
 */
@RestController
@RequestMapping("/thmlfApi/avs/hkpay")
@Api("汇卡支付")
public class AvsHkPayAction {

    @Autowired
    private AvsHkPayBusiness hkPayBusiness;

    @Autowired
    private CityService cityService;

    @Autowired
    private BankService bankService;

    @PostMapping("/createWechatPayUrl")
    @ApiOperation(value = "创建微信支付", notes = "创建微信支付", httpMethod = "POST", response = RestResponse.class)
    public RestResponse createWechatPayUrl(@RequestBody HkCreateWechatOrderRequest request) throws UnsupportedEncodingException {
        return hkPayBusiness.createWechatPayUrl(request.getCashBindId(), request.getOrderNum(), request.getPayerId());
    }

    @GetMapping("/createWechatOrder")
    @ApiOperation(value = "创建微信支付订单", notes = "创建微信支付订单", httpMethod = "GET", response = RestResponse.class)
    public RestResponse createWechatOrder(String cashBindId, String orderNum, String payerId, String type, String openId) {
        return hkPayBusiness.createOrder(cashBindId, orderNum, payerId, type, openId);
    }

    @PostMapping("/createorder")
    @ApiOperation(value = "创建支付订单", notes = "创建支付订单", httpMethod = "POST", response = RestResponse.class)
    public RestResponse createOrder(@RequestBody HkACreateOrderRequest request) {
        return hkPayBusiness.createOrder(request.getCashBindId(),request.getOrderNum(),request.getPayerId(),request.getType(),request.getOpenId());
    }

    @PostMapping("/paymentnotify")
    @ApiOperation(value = "支付通知", notes = "支付通知", httpMethod = "POST", response = RestResponse.class)
    public void paymentNotify(HttpServletRequest request) {
        hkPayBusiness.paymentNotify(request);
    }

    @GetMapping("/getWechatCode")
    @ApiOperation(value = "获取微信Code", notes = "创建微信支付订单", httpMethod = "GET", response = RestResponse.class)
    public RestResponse getCode() {
        return hkPayBusiness.getCode();
    }

    @GetMapping("/openId")
    @ApiOperation(value = "获取微信openId", notes = "创建微信支付订单", httpMethod = "GET", response = RestResponse.class)
    public void openId(String code, String uri, HttpServletResponse response) {
        hkPayBusiness.getOpenId(code, uri, response);
    }

    @GetMapping("/provinces")
    @ApiOperation(value = "获取所有省份", notes = "获取所有省份", httpMethod = "GET", response = RestResponse.class)
    public RestResponse provinces() {
        return RestResponse.build(cityService.allProvinces());
    }

    @GetMapping("/citys")
    @ApiOperation(value = "获取省份下面所有市", notes = "获取省份下面所有市", httpMethod = "GET", response = RestResponse.class)
    public RestResponse citys(String proNo) {
        return RestResponse.build(cityService.cityByProvince(proNo));
    }

    @GetMapping("/banks")
    @ApiOperation(value = "获取所有银行", notes = "获取所有银行", httpMethod = "GET", response = RestResponse.class)
    public RestResponse banks() {
        return RestResponse.build(bankService.allBanks());
    }

    @GetMapping("/clearBanks")
    @ApiOperation(value = "获取银行开户行", notes = "获取银行开户行", httpMethod = "GET", response = RestResponse.class)
    public RestResponse clearBanks(Integer bankNo) {
        return RestResponse.build(bankService.bankCodeByBank(bankNo));
    }

    @PostMapping("/submitMerInfo")
    @ApiOperation(value = "提交商户信息", notes = "商户注册信息提交", httpMethod = "POST")
    public RestResponse submitInfo(@RequestBody HkSubmitRequest request) {
        return hkPayBusiness.submitMerInfo(request);
    }

    @GetMapping(value = "/userNameWithIdCard")
    @ApiOperation(value = "获取用户名及身份证", notes = "获取用户名及身份证", httpMethod = "GET")
    public RestResponse userNameWithIdCard(String payerId){
        return hkPayBusiness.userNameWithIdCard(payerId);
    }

    @GetMapping(value = "/userMerRegInfo")
    @ApiOperation(value = "获取用户注册情况", notes = "获取用户注册情况", httpMethod = "GET")
    public RestResponse userMerRegInfo(String payerId){
        return hkPayBusiness.userMerRegInfo(payerId);
    }

    @RequestMapping(value = "/merPhotoUpload", method = RequestMethod.POST)
    @ApiOperation(value = "提交商户照片", notes = "提交商户照片", httpMethod = "POST")
    public RestResponse submitPhoto(@Valid HkSubmitMerPhotoRequest request, BindingResult result) {
        return hkPayBusiness.submitPhoto(request, result);
    }

    @RequestMapping(value = "/searchBankWithCity",method = RequestMethod.GET)
    @ApiOperation(value = "查询开户行列表", notes = "根据开户行所在城市和银行名称查询开户行列表", httpMethod = "GET")
    public RestResponse searchBanks(String bankName){
        return RestResponse.build(bankService.searchBanks(bankName));
    }

}
